package com.example.qizhong;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity{


    private TextView textView1,textView2,textView3,textView4,textView5,textView6,textView7,textView8;
    private EditText email,password1,password2,nicknane;
    private Button button;
    private AlertDialog alertDialog;
    private Spinner provinceSpinner = null;//省级（省、直辖市）
    private Spinner citySpinner = null;//地级市
    private Spinner language;  //选择语言
    ArrayAdapter<String> provinceAdapter = null;//省级适配器
    ArrayAdapter<String> cityAdapter = null;//地级适配器
    private String[] province = new String[]{"北京", "上海", "天津", "广东", "香港", "澳门"};
    private String[][] city = new String[][]{
            {"不限", "东城区", "西城区", "崇文区", "宣武区", "朝阳区", "海淀区", "丰台区", "石景山区", "门头沟区", "房山区", "通州区", "顺义区", "大兴区", "昌平区", "平谷区", "怀柔区", "密云县", "延庆县"},//北京市
            {"不限", "长宁区", "静安区", "普陀区", "闸北区", "虹口区"},//上海市
            {"不限", "和平区", "河东区", "河西区", "南开区", "河北区", "红桥区", "塘沽区", "汉沽区", "大港区", "东丽区"},//天津市
            {"不限", "广州", "深圳", "湛江", "韶关", "珠海", "汕头", "佛山", "肇庆", "江门", "茂名", "惠州", "梅州", "汕尾", "河源", "阳江", "清远", "东莞", "中山", "潮州", "揭阳", "云浮"},//广东省
            {"全香港"},//香港
            {"全澳门"}//澳门
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setSpinner();
        textView1 = (TextView)findViewById(R.id.textView);
        textView2 = (TextView)findViewById(R.id.textView2);
        textView3 = (TextView)findViewById(R.id.textView3);
        textView4 = (TextView)findViewById(R.id.textView4);
        textView5 = (TextView)findViewById(R.id.textView5);
        textView6 = (TextView)findViewById(R.id.textView6);
        textView7 = (TextView)findViewById(R.id.textView7);
        textView8 = (TextView)findViewById(R.id.textView8);
        language = (Spinner)findViewById(R.id.spinner);
        email = (EditText)findViewById(R.id.email);   //邮箱输入框
        password1 = (EditText)findViewById(R.id.password1);  //密码输入框
        password2 = (EditText)findViewById(R.id.password2);   //确认密码
        button = (Button)findViewById(R.id.button);
        nicknane = (EditText) findViewById(R.id.nickname);  //昵称


        //判断邮箱是否合法
        email.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                String pattern = "^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$";
                String text = email.getText().toString();
                if(!text.matches(pattern)){
                    email.setError("邮箱格式错误！");
                }

            }
        });


        //判断密码是否合法
        password1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                String pw = password1.getText().toString();
                if(pw==null || pw.trim().equals("")){
                    password1.setError("请输入内容！");
                }else if(pw.length()<6 || pw.length()>15){
                    password1.setError("密码长度必须为6—15位！");
                }
            }
        });

        //判断两次输入的密码是否一致
        password2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                String pw2 = password2.getText().toString();
                if (!pw2.equals(password1.getText().toString())){
                    password2.setError("两次输入的密码不一致！");
                }
            }
        });

        //点击立即开通按钮后弹出确认信息对话框
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setAlertDialog();
                alertDialog.setMessage("邮箱:"+email.getText().toString()+'\n'
                +"密码:"+password1.getText().toString()+'\n'
                +"昵称:"+nicknane.getText().toString()+'\n'
                +"所在地:"+provinceSpinner.getSelectedItem().toString()+'\t'+citySpinner.getSelectedItem().toString());
                alertDialog.show();
            }
        });


        //下拉框选择界面语言
        language.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                switch (position){
                    case 0:
                        textView1.setText("邮箱:");
                        textView2.setText("创建密码:");
                        textView3.setText("确认密码:");
                        textView4.setText("昵称:");
                        textView5.setText("所在地:");
                        textView6.setText("验证码:");
                        textView7.setText("我没有邮箱");
                        textView8.setText("换一换");
                        button.setText("立即开通");
                        break;
                    case 1:
                        textView1.setText("Email:");
                        textView2.setText("Password:");
                        textView3.setText("Enter again:");
                        textView4.setText("Nickname:");
                        textView5.setText("Location:");
                        textView6.setText("Code:");
                        textView7.setText("No Email");
                        textView8.setText("Change");
                        button.setText("Register Now");
                        break;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    private void setSpinner(){

        provinceSpinner = (Spinner) findViewById(R.id.spin_province);
        citySpinner = (Spinner) findViewById(R.id.spin_city);
        //绑定provinceAdapter适配器和值
        provinceAdapter = new ArrayAdapter<String>
                (MainActivity.this, android.R.layout.simple_spinner_item, province);
        provinceSpinner.setAdapter(provinceAdapter);
        provinceSpinner.setSelection(3, true);  //设置默认选中项，此处为默认选中第4个值
        //绑定cityAdapter适配器和值
        cityAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, city[3]);
        citySpinner.setAdapter(cityAdapter);
        citySpinner.setSelection(0, true);  //默认选中第0个

        //spinner的级联操作
        provinceSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            // 表示选项被改变的时候触发此方法，主要实现办法：动态改变地级适配器的绑定值
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                //position为当前省级选中的值的序号
                // 将地级适配器的值改变为city[position]中的值
                cityAdapter = new ArrayAdapter<String>(
                        MainActivity.this, android.R.layout.simple_spinner_item, city[position]);
                // 设置二级下拉列表的选项内容适配器
                citySpinner.setAdapter(cityAdapter);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });



    }

    //初始化对话框
    private void setAlertDialog(){
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                .setTitle("确认信息")
                .setIcon(R.mipmap.queren)
                .setMessage("")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();  //关闭对话框
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();  //关闭对话框
                    }
                });
        alertDialog = builder.create();
       // alertDialog.show();
    }

}